package com.yisu.expressway.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.yisu.expressway.R;
import com.yisu.expressway.utils.StringUtil;


/**
 * Created by theodore_hu on 2016/4/13.
 */
public class SingleButtonAlertDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private ViewHolder viewHolder;

    private PositiveClickListener mListener;

    public interface PositiveClickListener{
        void onPositiveClick();
    }

    public SingleButtonAlertDialog(Context context) {
        super(context, R.style.DialogLayout);
    }

    public SingleButtonAlertDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }

    public void setPositiveClickListener(PositiveClickListener listener){
        this.mListener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_single_button_layout);
        viewHolder = new ViewHolder();
    }

    public void setContentText(String text){
        if(!StringUtil.isNullOrEmpty(text)){
            viewHolder.tv_content.setText(text);
            viewHolder.tv_content.post(new Runnable() {
                @Override
                public void run() {
                    int lineCount =  viewHolder.tv_content.getLineCount();
                    if(lineCount == 1){
                        viewHolder.tv_content.setGravity(Gravity.CENTER);
                    }
                }
            });

        }
    }
    public void setButtonBgColor(int colorResId){
        viewHolder.tv_ok.setBackgroundColor(getContext().getResources().getColor(colorResId));
    }
    public void setContentText(int stringId){
        viewHolder.tv_content.setText(stringId);
        viewHolder.tv_content.post(new Runnable() {
            @Override
            public void run() {
                int lineCount =  viewHolder.tv_content.getLineCount();
                if(lineCount == 1){
                    viewHolder.tv_content.setGravity(Gravity.CENTER);
                }
            }
        });
    }

    public void setTitle(int resId){
        viewHolder.mTv_title.setVisibility(View.VISIBLE);
        viewHolder.mTv_title.setText(resId);
    }


    class ViewHolder {


        TextView mTv_title;
        TextView tv_ok;
        public TextView tv_content;

        public ViewHolder(){
            mTv_title = (TextView)findViewById(R.id.tv_title);
            tv_content = (TextView)findViewById(R.id.tv_content);
            tv_ok = (TextView) findViewById(R.id.tv_ok);
            tv_ok.setOnClickListener(SingleButtonAlertDialog.this);

        }
    }

    public void close(){
        dismiss();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_ok:
                if(mListener != null){
                    mListener.onPositiveClick();
                }else {
                    close();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            close();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}
